package com.poinrti.emoSurvivorM;

import java.util.concurrent.ConcurrentLinkedQueue;

import com.pointi.emoGatewayM.JDBC;
import com.pointi.emoTools.infoStatus;

public class ThreadCheckSurvivor extends Thread
{
	//____________________________________________________________________________________________________
	// DEF
	
	private ConcurrentLinkedQueue<ThreadCheckSurvivor> activeThreads ;
	private JDBC jdbc ;
	private int LICENSE = 100 ;
	private int CHK_INTERVAL = 50000 ;

	//____________________________________________________________________________________________________
	// INIT
	
	public ThreadCheckSurvivor(ConcurrentLinkedQueue<ThreadCheckSurvivor> inThread)
	{
		activeThreads = inThread ;
	}

	public void start(JDBC inJDBC)
	{
		jdbc = inJDBC ;
		activeThreads.add(this) ;
		super.start() ;
	}
	
	//____________________________________________________________________________________________________
	// RUN // Checker

	public void run()
	{
		//____________________________________________________________________________________________________

		int si = 0 ; // Server Index
		int ci = 0 ; // Client Index

		infoStatus [] iSVR = new infoStatus[LICENSE] ; // info Server
		infoStatus [] iCRT = new infoStatus[LICENSE] ; // info Client
		infoStatus [] tSVR = new infoStatus[LICENSE] ; // temp Server
		infoStatus [] tCRT = new infoStatus[LICENSE] ; // temp Client

		//____________________________________________________________________________________________________

		try
		{
			boolean loopHandler = true ;
			while (loopHandler)
			{
				//____________________________________________________________________________________________________
				// Check Server Info.
				
				si = jdbc.fnGetInfoServer(tSVR) ;
				for (int i=0; i<si; i++) // Loop New Status
				{
					for (int ii=0; ii<LICENSE; ii++) // Loop Old Status
					{
						if (tSVR[i].no == iSVR[ii].no)
						{
							if (tSVR[i].status > iSVR[ii].status) // SERVER.i Alive
							{
								iSVR[ii].status = tSVR[i].status ;
							}
							else if ((tSVR[i].status == iSVR[ii].status) && (iSVR[ii].alive == infoStatus.ALIVE)) // SERVER.i Just Die ..
							{
								iSVR[ii].alive = infoStatus.DIE ;
								jdbc.fnReassignServer(iSVR[ii].no) ;
							}
							else if ((tSVR[i].status < iSVR[ii].status) && (iSVR[ii].alive == infoStatus.DIE)) // SERVER.i Rebirth !
							{
								iSVR[ii].alive = infoStatus.ALIVE ;
								iSVR[ii].status = tSVR[i].status ;
							}
							jdbc.fnUpdateInfoServer(iSVR[ii]) ;
						}
					}
				}

				//____________________________________________________________________________________________________
				// Check Client Info.
				
				ci = jdbc.fnGetInfoClient(tCRT) ;
				for (int i=0; i<ci; i++) // Loop New Status
				{
					for (int ii=0; ii<LICENSE; ii++) // Loop Old Status
					{
						if (tCRT[i].no == iCRT[ii].no)
						{
							if (tCRT[i].status > iCRT[ii].status) // SERVER.i Alive
							{
								iCRT[ii].status = tCRT[i].status ;
							}
							else if ((tCRT[i].status == iCRT[ii].status) && (iCRT[ii].alive == infoStatus.ALIVE)) // SERVER.i Just Die ..
							{
								iCRT[i].alive = infoStatus.DIE ;
								jdbc.fnReassignClient(iCRT[ii].no) ;
							}
							else if ((tCRT[i].status < iCRT[ii].status) && (iCRT[ii].alive == infoStatus.DIE)) // SERVER.i Rebirth !
							{
								iCRT[ii].alive = infoStatus.ALIVE ;
								iCRT[ii].status = tCRT[i].status ;
							}
							jdbc.fnUpdateInfoClient(iCRT[ii]) ;
						}
					}
				}
			}
			Thread.sleep(CHK_INTERVAL) ; // Next CHK, after 5(s)
		}
		catch (Exception e) { e.printStackTrace() ; } 
		activeThreads.remove(this) ;
	}
	
	//____________________________________________________________________________________________________
	// EOC
}